jqapi = function() {
  var elements = {};
  
  var values = {
    searchHeight:   null,
    selected:       'selected',
    category:       'category',
    open:           'open',
    catSelected:    'cat-selected',
    sub:            'sub',
    hasFocus:       true,
    loader:         '<div id="loader"></div>',
    title:          'hive 1.0 '
  };
  
  var keys = {
    enter:  13,
    escape: 27,
    up:     38,
    down:   40,
    array:  [13, 27, 38, 40]
  }
  
  
  function initialize() {
    elements = {
      search:         $('#search-field'),
      searchWrapper:  $('#search'),
      content:        $('#content'),
      list:           $('#static-list'),
      window:         $(window),
      results:        null,
      category:       null
    };
    
    elements.results    = jQuery('<ul>', { id: 'results' }).insertBefore(elements.list);
    elements.category   = $('.category', elements.list);
    values.searchHeight = elements.searchWrapper.innerHeight();

    elements.window.resize(function() {
      var winH =  elements.window.height();
      var listH = winH - values.searchHeight;
      
      elements.list.height(listH);
      elements.results.height(listH);
      elements.content.height(winH);
      elements.search.width(elements.searchWrapper.width() - 8);
    })
    .mousemove(function(event) {
      if(event.pageX < elements.list.width()) searchFocus();
    })
    .keydown(function(event) {
      if(event.keyCode == keys.escape) {
        elements.search.val('').focus();
        elements.results.hide();
        elements.list.show();
      }
    })
    .trigger('resize'); //trigger resize event to initially set sizes
    
    elements.search.keyup(function(event) {
      if($.inArray(event.keyCode, keys.array) != -1) { //it is an event key
        handleKey(event.keyCode);
      } else { //it is a character
        startSearch();
      }
    })
    .focus(function() {
      values.hasFocus = true;
    })
    .blur(function() {
      values.hasFocus = false;
    })
    .focus();
    
    $('.'+values.category+' > span', elements.list).toggle(function() {
      clearSelected();
      searchFocus();
      $(this).parent().addClass(values.open).children('ul').show();
    }, function() {
      clearSelected();
      searchFocus();
      $(this).parent().removeClass(values.open).children('ul').hide();
    });
    
    $('.sub a').live('click', function() {
      var el = $(this);
      clearSelected();
      searchFocus();
      el.parent().addClass(values.selected);
    
        demoBind({ n: urlMethodName(el) });
      return false;
    });
    

    
    zebraItems(elements.list); //zebra the items in the static list
  } //-initialize
  
	function startSearch(){}
  function demoBind(obj){
	 
	 $.post('/hive?a=as&i='+obj.n,function(c){
		$("#content").html(c);
		SyntaxHighlighter.highlight();
	 });
  }
  
  function searchFocus() {
    elements.search.focus();
  } //-searchFocus
  
  
  function zebraItems(list) {
    $('.sub:odd', list).addClass('odd');
  } //-zebraItems
  
  
  function clearSelected() {
    $('.'+values.selected).removeClass(values.selected);
  } //-clearSelected
  
  
  
  function urlMethodName(link) {
    var href = link.attr('href');
    return href.substr(1, href.length);
  } //-urlMethodName

   function urlMethodExt(link) {
    var href = link.attr('href');
    return href.getExt();
  } //-urlMethodExt
  
  
  
  
  
  return {
    initialize: initialize
  }
}();

String.prototype.getExt = function(){
	var ext = this.slice(this.lastIndexOf('.')+1,this.length);
	return ext;
}

$(document).ready(function() {
    jqapi.initialize();
	$('#navigation').hover(
		function () {
			$(this).stop().animate({'left':'0'},200);
			$("#footer").fadeIn();
			$("#content").css('width','82%')
		},
		function () {
			$(this).stop().animate({'left':'-16%'},200);
			$("#footer").hide();
			$("#content").css('width','98%')
		}
	);
});
